package ltd.dujiabao.capability.domain.rulemng;

import java.util.Collections;
import java.util.List;

import ltd.dujiabao.capability.domain.capmng.cap.Capability;
import ltd.dujiabao.capability.domain.rulemng.expression.Expression;

/**
 * @author : DuJiabao
 * @Project : ddd-capability
 * @Time : 2024/4/13 12:24
 * @Desc :
 */
public class VersionRule {

    private final String version;

    private final List<Expression> and;

    private final List<Expression> or;

    public VersionRule(String version, List<Expression> and, List<Expression> or) {
        this.version = version;
        this.and = Collections.unmodifiableList(and);
        this.or = Collections.unmodifiableList(or);
    }

    public String getVersion() {
        return version;
    }

    public List<Expression> getAnd() {
        return and;
    }

    public List<Expression> getOr() {
        return or;
    }

    public boolean checkIfSupport(Capability capability) {
        // 检查or
        boolean result = or.isEmpty();
        for (Expression expression : or) {
            result |= expression.check(capability);
        }
        if (!result) {
            return false;
        }

        // 检查and
        for (Expression expression : and) {
            result &= expression.check(capability);
        }
        return result;
    }
}
